#if defined(TSINGMA_MX) || defined(TSINGMA_GX) || defined(ARCTIC)
#if (FEATURE_MODE == 0)
/**
 @file sys_tmm_oam.c


 @author  Copyright (C) 2020 Centec Networks Inc.  All rights reserved.

 @date 2020-03-17

 @version v1.0

*/

/****************************************************************************
 *
* Header Files
*
****************************************************************************/
#include "ctc_oam.h"
#include "ctc_error.h"
#include "sys_usw_oam_db.h"

extern sys_oam_master_t* g_oam_master[CTC_MAX_LOCAL_CHIP_NUM_PP];
int32
sys_tmm_oam_get_mep_info_for_event(uint8 lchip, void* p_mep_info)
{
    uint32 lmep_index   = 0;
    uint32 cmd_mep  = 0;
    uint32 cmd_rmep = 0;
    ctc_oam_mep_type_t mep_type = 0;
    DsEthMep_m   ds_mep;
    DsEthMep_m   ds_rmep;
    sys_oam_lmep_t* p_sys_lmep_com  = NULL;
    sys_oam_rmep_t* p_sys_rmep_com  = NULL;
    ctc_oam_y1731_lmep_info_t*  p_lmep_y1731_info = NULL;
    ctc_oam_y1731_rmep_info_t*  p_rmep_y1731_info = NULL;
    ctc_oam_bfd_lmep_info_t*    p_lmep_bfd_info = NULL;
    ctc_oam_bfd_rmep_info_t*    p_rmep_bfd_info = NULL;
    ds_eth_mep_tmm_t* ds_eth_mep = NULL;
    ds_eth_rmep_tmm_t* ds_eth_rmep = NULL;
    ds_bfd_mep_tmm_t* ds_bfd_mep = NULL;
    ds_bfd_rmep_tmm_t* ds_bfd_rmep = NULL;  
    uint8 is_remote = 0;
    uint8 d_sf=0;
    ctc_oam_mep_info_t* mep_info = p_mep_info;    

    cmd_mep     = DRV_IOR(DsEthMep_t,   DRV_ENTRY_FLAG);
    cmd_rmep    = DRV_IOR(DsEthRmep_t,  DRV_ENTRY_FLAG);
    CTC_ERROR_RETURN(DRV_IOCTL(lchip, mep_info->mep_index, DRV_CMD_PP_EN(cmd_rmep), &ds_rmep));

    ds_eth_mep = (ds_eth_mep_tmm_t*)(&ds_mep);
    ds_eth_rmep = (ds_eth_rmep_tmm_t*)(&ds_rmep);
    ds_bfd_mep = (ds_bfd_mep_tmm_t*)(&ds_mep);
    ds_bfd_rmep = (ds_bfd_rmep_tmm_t*)(&ds_rmep);
    mep_info->is_rmep = ds_eth_rmep->isRemote;
    if (!ds_eth_rmep->active)
    {
        return CTC_E_NOT_READY;
    }

    if (mep_info->is_rmep == 1)
    {
        p_sys_rmep_com = ctc_vector_get(g_oam_master[lchip]->mep_vec, mep_info->mep_index);
        if (p_sys_rmep_com)
        {
            mep_type = p_sys_rmep_com->p_sys_lmep->p_sys_chan->mep_type;
        }
        else
        {
            return CTC_E_INVALID_PARAM;
        }

        is_remote = 1;

        if ((CTC_OAM_MEP_TYPE_ETH_1AG == mep_type)
            || (CTC_OAM_MEP_TYPE_ETH_Y1731 == mep_type)
            || (CTC_OAM_MEP_TYPE_MPLS_TP_Y1731 == mep_type))
        {
            p_rmep_y1731_info = &(mep_info->rmep.y1731_rmep);
            p_rmep_y1731_info->rmep_id = p_sys_rmep_com->rmep_id;
            p_rmep_y1731_info->active                  = 1;
            p_rmep_y1731_info->first_pkt_rx        = ds_eth_rmep->firstPktRx;
            p_rmep_y1731_info->d_loc                   = ds_eth_rmep->dLoc;
            p_rmep_y1731_info->d_unexp_period   = ds_eth_rmep->dUnexpPeriod;
            p_rmep_y1731_info->ma_sa_mismatch   = ds_eth_rmep->rmepMacMismatch;
            p_rmep_y1731_info->mac_status_change = ds_eth_rmep->macStatusChange;
            p_rmep_y1731_info->last_rdi         = ds_eth_rmep->rmepLastRdi;
            p_rmep_y1731_info->is_p2p_mode = ds_eth_rmep->ethOamP2PMode;
            p_rmep_y1731_info->lmep_index = p_rmep_y1731_info->is_p2p_mode ? (mep_info->mep_index - 1) :
                                                                       ds_eth_rmep->u3_;
            lmep_index = p_rmep_y1731_info->lmep_index;
            if (p_rmep_y1731_info->is_p2p_mode && ds_eth_rmep->isCsfEn)
            {
                p_rmep_y1731_info->csf_en       = 1;
                p_rmep_y1731_info->d_csf        = (ds_eth_rmep->u1__15_31>>14)&0x1;
                p_rmep_y1731_info->rx_csf_type  = (((ds_eth_rmep->u1__0_14>>13) & 0x3) | 
                                                   ((ds_eth_rmep->u1__15_31 & 0x1)<<2));
            }
            if (g_oam_master[lchip]->sf_defect_bitmap && g_oam_master[lchip]->sf_high_pri)
            {
                /*Ethoam rmep*/
                if (g_oam_master[lchip]->sf_defect_bitmap & CTC_OAM_DEFECT_UNEXPECTED_PERIOD)
                {
                    d_sf |= p_rmep_y1731_info->d_unexp_period;
                }
                if (g_oam_master[lchip]->sf_defect_bitmap & CTC_OAM_DEFECT_DLOC)
                {
                    d_sf |= p_rmep_y1731_info->d_loc;
                }
                if (g_oam_master[lchip]->sf_defect_bitmap & CTC_OAM_DEFECT_EVENT_RDI_RX)
                {
                    d_sf |= p_rmep_y1731_info->last_rdi;
                }
                if (g_oam_master[lchip]->sf_defect_bitmap & CTC_OAM_DEFECT_MAC_STATUS_CHANGE)
                {
                    d_sf |= p_rmep_y1731_info->mac_status_change;
                }
            }
        }
        else if ((CTC_OAM_MEP_TYPE_IP_BFD == mep_type)
                    || (CTC_OAM_MEP_TYPE_MPLS_BFD == mep_type)
                    || (CTC_OAM_MEP_TYPE_MPLS_TP_BFD == mep_type)
                    || (CTC_OAM_MEP_TYPE_MICRO_BFD == mep_type))
        {
            p_rmep_bfd_info = &(mep_info->rmep.bfd_rmep);
            p_rmep_bfd_info->first_pkt_rx = ds_bfd_rmep->firstPktRx;
            p_rmep_bfd_info->mep_en = ds_bfd_rmep->active;
            p_rmep_bfd_info->timer_neg_ok = ds_bfd_rmep->timeNegDone;
            p_rmep_bfd_info->required_min_rx_interval = ds_bfd_rmep->requiredMinRxInterval;
            p_rmep_bfd_info->actual_rx_interval = ds_bfd_rmep->actualRxInterval;
            if (g_oam_master[lchip]->sf_defect_bitmap && g_oam_master[lchip]->sf_high_pri)
            { 
                if (g_oam_master[lchip]->sf_defect_bitmap & CTC_OAM_DEFECT_EVENT_BFD_MIS_CONNECT)
                {
                    d_sf |= p_rmep_bfd_info->mis_connect;
                }
            }
            if (p_rmep_bfd_info->timer_neg_ok)
            {
                DsBfdRmep_m ds_bfd_rmep1;
                DsBfdRmep_m ds_bfd_rmep_mask1;
                tbl_entry_t tbl_entry = {0};
                sal_memset(&ds_bfd_rmep1, 0, sizeof(DsBfdRmep_m));
                sal_memset(&ds_bfd_rmep_mask1, 0xFF, sizeof(DsBfdRmep_m));
                SetDsBfdRmep(V, timeNegDone_f, &ds_bfd_rmep1, 0);
                SetDsBfdRmep(V, timeNegDone_f, &ds_bfd_rmep_mask1, 0);
                cmd_rmep = DRV_IOW(DsEthRmep_t, DRV_ENTRY_FLAG);
                tbl_entry.data_entry = (uint32*)&ds_bfd_rmep1;
                tbl_entry.mask_entry = (uint32*)&ds_bfd_rmep_mask1;
                CTC_ERROR_RETURN(DRV_IOCTL(lchip, mep_info->mep_index, DRV_CMD_PP_EN(cmd_rmep), &tbl_entry));
            }
            if (CTC_OAM_MEP_TYPE_MPLS_TP_BFD == mep_type)
            {
                p_rmep_bfd_info->mis_connect        = CTC_IS_BIT_SET(ds_bfd_rmep->u1__0_30, 0);
            }
            p_rmep_bfd_info->remote_detect_mult = ds_bfd_rmep->defectMult;
            p_rmep_bfd_info->remote_discr = (ds_bfd_rmep->u2__0_30 | ((ds_bfd_rmep->u2__31_31 & 0x1)<<31));
            p_rmep_bfd_info->remote_diag = ds_bfd_rmep->remoteDiag;
            p_rmep_bfd_info->remote_state = ds_bfd_rmep->remoteStat;
            p_rmep_bfd_info->lmep_index  = (mep_info->mep_index - 1);
            lmep_index = p_rmep_bfd_info->lmep_index;
        }
    }
    else
    {
        lmep_index = mep_info->mep_index;
    }

    if(is_remote)
    {
        CTC_ERROR_RETURN(DRV_IOCTL(lchip, lmep_index, DRV_CMD_PP_EN(cmd_mep), &ds_mep));
    }
    else
    {
        sal_memcpy(&ds_mep, &ds_rmep, sizeof(DsEthMep_m));
    }

#if (SDK_WORK_PLATFORM == 1)
    if (!ds_eth_mep->active)
    {
        return CTC_E_NONE;
    }
#endif
    p_sys_lmep_com = (sys_oam_lmep_t*)ctc_vector_get(g_oam_master[lchip]->mep_vec, lmep_index);
    if (p_sys_lmep_com)
    {
        if (p_sys_lmep_com->p_sys_chan)
        {
            mep_type = p_sys_lmep_com->p_sys_chan->mep_type;
        }
        mep_info->mep_type = mep_type;
    }
    else
    {
        return CTC_E_INVALID_PARAM;
    }

    if ((CTC_OAM_MEP_TYPE_ETH_1AG == mep_type)
        || (CTC_OAM_MEP_TYPE_ETH_Y1731 == mep_type)
        || (CTC_OAM_MEP_TYPE_MPLS_TP_Y1731 == mep_type))
    {
         /* labter complete for gg eth-oam*/
        p_lmep_y1731_info = &(mep_info->lmep.y1731_lmep);
        p_lmep_y1731_info->mep_id         = ds_eth_mep->mepId;
        p_lmep_y1731_info->active         = 1;
        p_lmep_y1731_info->d_unexp_mep    = ds_eth_mep->dUnexpMep;
        p_lmep_y1731_info->d_mismerge     = ds_eth_mep->dMismerge;
        p_lmep_y1731_info->d_meg_lvl      = ds_eth_mep->dMegLvl;
        p_lmep_y1731_info->vlan_id        = ds_eth_mep->mepPrimaryVid;
        p_lmep_y1731_info->present_rdi    = ds_eth_mep->presentRdi;
        p_lmep_y1731_info->level = p_sys_lmep_com->md_level;
        if (g_oam_master[lchip]->sf_defect_bitmap && g_oam_master[lchip]->sf_high_pri)
        {
            /*Ethoam lmep*/
            if (g_oam_master[lchip]->sf_defect_bitmap & CTC_OAM_DEFECT_UNEXPECTED_MEP)
            {
                d_sf |= p_lmep_y1731_info->d_unexp_mep;
            }
            if (g_oam_master[lchip]->sf_defect_bitmap & CTC_OAM_DEFECT_MISMERGE)
            {
                d_sf |= p_lmep_y1731_info->d_mismerge;
            }
            if (g_oam_master[lchip]->sf_defect_bitmap & CTC_OAM_DEFECT_UNEXPECTED_LEVEL)
            {
                d_sf |= p_lmep_y1731_info->d_meg_lvl;
            }
        }
    }
    else if ((CTC_OAM_MEP_TYPE_IP_BFD == mep_type)
                    || (CTC_OAM_MEP_TYPE_MPLS_BFD == mep_type)
                    || (CTC_OAM_MEP_TYPE_MPLS_TP_BFD == mep_type)
                    || (CTC_OAM_MEP_TYPE_MICRO_BFD == mep_type))
    {
        p_lmep_bfd_info = &(mep_info->lmep.bfd_lmep);
        p_lmep_bfd_info->mep_en =  ds_bfd_mep->active;
        p_lmep_bfd_info->loacl_state = ds_bfd_mep->localStat;
        p_lmep_bfd_info->local_diag = ds_bfd_mep->localDiag;
        p_lmep_bfd_info->local_discr = p_sys_lmep_com->local_discr;    /*get from db, can not get from asic*/

        if (CTC_OAM_MEP_TYPE_MPLS_TP_BFD == mep_type)
        {
            if (!is_remote)
            {   
                uint32 cmd = 0;
                cmd = DRV_IOR(DsBfdRmep_t,  DRV_ENTRY_FLAG);
                CTC_ERROR_RETURN(DRV_IOCTL(lchip, lmep_index + 1, DRV_CMD_PP_EN(cmd), ds_bfd_rmep));
                mep_info->rmep.bfd_rmep.mis_connect        = CTC_IS_BIT_SET(ds_bfd_rmep->u1__0_30, 0);
            }
            else if (ds_bfd_rmep->isCsfEn)
            {
                p_lmep_bfd_info->csf_en       = 1;
                p_lmep_bfd_info->d_csf        = (ds_bfd_mep->u2_ >> 12)&0x1;
                p_lmep_bfd_info->rx_csf_type  = (ds_bfd_mep->u2_ >> 13)&0x7;
            }
            p_lmep_bfd_info->cv_enable = ds_bfd_rmep->isCvEn;
            if (p_sys_lmep_com->p_sys_maid && p_lmep_bfd_info->cv_enable)
            {
               sal_memcpy(&p_lmep_bfd_info->mep_id, &p_sys_lmep_com->p_sys_maid->maid, p_sys_lmep_com->p_sys_maid->maid_len);
            }
        }
        if (g_oam_master[lchip]->sf_defect_bitmap && g_oam_master[lchip]->sf_high_pri)
        {
            if (g_oam_master[lchip]->sf_defect_bitmap & CTC_OAM_DEFECT_EVENT_BFD_DOWN)
            {
                d_sf |= (CTC_OAM_BFD_STATE_DOWN == p_lmep_bfd_info->loacl_state)? 1 : 0;
            }
        }
    }
    return d_sf;
}




#endif
#endif
